﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.PhongMau;

namespace QuanLyKho.PhongThanhPham
{
    public partial class Thanhpham_DanhsachSP : UserControl
    {
        public Thanhpham_DanhsachSP()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            DataTable data = DataAll.SelectAll("HoanThien inner join PhongMau_SanPham on HoanThien.IdSanPham = PhongMau_SanPham.IdSanPham",
                       "PhongMau_SanPham.IdSanPham,PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],HoanThien.[ID],HoanThien.[IdSanPham],HoanThien.[NgayLapLenh],HoanThien.[NguoiCapNhatLenh],HoanThien.[SoLuong],HoanThien.[NgayGuiLenh],HoanThien.[NgaySuaLenh],HoanThien.[TrangThai],HoanThien.[NgayChungTu],HoanThien.[NgayHoachToan],HoanThien.[SoChungTu],HoanThien.[GhiChu],HoanThien.[KhoDen],HoanThien.[KhoDi],HoanThien.[SXTuNgay],HoanThien.[SXDenNgay]",
                   "WHERE HoanThien.TrangThai = " + Convert.ToInt32(TrangThai.HoanThien_DaGui));
            gridHoanThien.DataSource = data;

            var table = DataAll.SelectAll("PhongMau_SanPham", "*", "WHERE TrangThai = " + Convert.ToInt32(TrangThai.PhongMau_ThanhPham));
            var imconverter = new ImageConverter();
            var columns = new DataColumn("GuiDuyet", typeof(byte[]));
            columns.DefaultValue = imconverter.ConvertTo(Properties.Resources.export_gray, typeof(byte[]));
            table.Columns.Add(columns);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (Convert.ToInt32(table.Rows[i]["TrangThai"]) == Convert.ToInt32(TrangThai.PhongMau_TaoMoi)
                    || Convert.ToInt32(table.Rows[i]["TrangThai"]) == Convert.ToInt32(TrangThai.PhongMau_KhongDuyet))
                {
                    table.Rows[i]["GuiDuyet"] = imconverter.ConvertTo(Properties.Resources.export, typeof(byte[]));
                }
            }
            gridSanPham.DataSource = table;
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            gvSanPham.BestFitColumns();
            gvSanPham.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvSanPham.OptionsBehavior.Editable = false;
            gvSanPham.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvSanPham.OptionsSelection.MultiSelect = true;
            gvSanPham.OptionsView.ShowGroupPanel = false;
            gvSanPham.OptionsView.ColumnAutoWidth = false;
            gvSanPham.OptionsView.ShowFooter = true;
            gvSanPham.OptionsFind.AlwaysVisible = true;
            gvSanPham.Columns["NguoiDangKy"].Fixed = FixedStyle.Left;

            gvHoanThien.BestFitColumns();
            gvHoanThien.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvHoanThien.OptionsBehavior.Editable = false;
            gvHoanThien.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvHoanThien.OptionsSelection.MultiSelect = true;
            gvHoanThien.OptionsView.ShowGroupPanel = false;
            gvHoanThien.OptionsView.ColumnAutoWidth = false;
            gvHoanThien.OptionsView.ShowFooter = true;
            gvHoanThien.OptionsFind.AlwaysVisible = true;
            gvHoanThien.Columns["ID"].Visible = false;
            for (int i = 2; i < gvSanPham.Columns.Count; i++)
            {
                gvSanPham.Columns[i].OptionsColumn.AllowEdit = false;
            }
            for (int i = 3; i < gvHoanThien.Columns.Count; i++)
            {
                gvHoanThien.Columns[i].OptionsColumn.AllowEdit = false;
            }
        }

        private void gvSanPham_DoubleClick(object sender, EventArgs e)
        {
            if (gvSanPham.FocusedRowHandle >= 0)
            {
                Phongmau_Themsanpham.MaID = gvSanPham.GetDataRow(gvSanPham.FocusedRowHandle)["IdSanPham"].ToString();
                Phongmau_Themsanpham.xem = false;
                var themsp = new Phongmau_Themsanpham();
                themsp.update = true;
                themsp.ShowDialog();
                LoadData();
                Phongmau_Themsanpham.MaID = "0";
                Phongmau_Themsanpham.xem = false;
            }
        }

        private void gvSanPham_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void btGuiThanhPham_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvHoanThien.DataSource;
            string a = gvHoanThien.GetRowCellValue(gvHoanThien.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.HoanThien_DaGui).ToString();
            if (a == b)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn cập nhật.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("HoanThien", "TrangThai =" + Convert.ToInt32(TrangThai.ThanhPham_CapNhat) + ",NgayGuiLenh ='" + DateTime.Now.ToString("MM/dd/yyyy") + "' WHERE ID =" + dataview[gvHoanThien.FocusedRowHandle]["ID"]);
                    message = DataAll.Update("PhongMau_SanPham", "TrangThai =" + Convert.ToInt32(TrangThai.PhongMau_ThanhPham) + " WHERE IdSanPham=" + dataview[gvHoanThien.FocusedRowHandle]["IdSanPham"]);
                    MessageBox.Show(message.Equals("success") ? "Cập nhật thành công" : message, "Message",
                                        MessageBoxButtons.OK,
                                        message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    DataAll.InsertAll("HanhTrinh", dataview[gvHoanThien.FocusedRowHandle]["MaSanPham"]
                            + "," + Convert.ToInt32(PhongBan.PhongHoanThien)
                            + "," + dataview[gvHoanThien.FocusedRowHandle]["SoChungTu"]
                            + "," + Convert.ToInt32(TrangThai.PhongMau_ThanhPham)
                            + "," + Main.UserID
                            + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                            + "," + dataview[gvHoanThien.FocusedRowHandle]["SoLuong"]
                            + "," + Convert.ToDateTime(dataview[gvHoanThien.FocusedRowHandle]["SXDenNgay"].ToString()).ToString("MM/dd/yyyy")
                            + "," + Convert.ToDateTime(dataview[gvHoanThien.FocusedRowHandle]["SXTuNgay"].ToString()).ToString("MM/dd/yyyy"));
                    LoadData();
                }
            }
        }

        private void btnHuy_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvHoanThien.DataSource;

            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn hủy ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("KCS", "TrangThai = " + Convert.ToInt32(TrangThai.KCS_DaDuyet) + " WHERE SoChungTu ='" + dataview[gvHoanThien.FocusedRowHandle]["SoChungTu"] + "'");
                if (message.Equals("success"))
                {
                    DataAll.Delete("HoanThien", "ID =" + dataview[gvHoanThien.FocusedRowHandle]["ID"]);
                }
                MessageBox.Show(message.Equals("success") ? "Hủy thành công" : message, "Message",
                                   MessageBoxButtons.OK,
                                   message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            }

            LoadData();
        }
    }
}
